﻿@using HZY.Infrastructure;
@using FreeSql.DatabaseModel;
@model HZY.Models.DTO.DevelopmentTool.GenContextDto
@{
    var className = Model.Name;
    var ignores = new string[] { "Id", "CreationTime","CreatorUserId", "LastModificationTime","LastModifierUserId" ,"DeletionTime","DeleterUserId", "IsDeleted" };
    var tableInfos = Model.Columns
        .Where(w => !ignores.Contains(w.Name))
        .ToList()
        ;
    var searchKeyWords = new[] {"Title", "Name", "Phone", "Address", "Email"};
    var searchKeyWord = string.Empty;
    foreach (var item in searchKeyWords)
    {
        if (tableInfos.Any(w => w.Name == item))
        {
            searchKeyWord = item;
            break;
        }
    }
}
<pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using @(Model.Namespace).EFCore.Models;
using @(Model.Namespace).Infrastructure;
using @(Model.Namespace).Models.Entities;
using @(Model.Namespace).Repositories;
using @(Model.Namespace).Repositories.Framework;
using @(Model.Namespace).Services.Admin.Framework;
using @(Model.Namespace).Services.Admin.BaseServicesAdmin;
using @(Model.Namespace).Services.Upload;
using Microsoft.AspNetCore.Http;

namespace @(Model.Namespace).Services.Admin
{
    /// <summary>
    /// 服务 @(className)Service
    /// </summary>
    public class @(className)Service : AdminBaseService<@($"{className}Repository")>
    {
        public @(className)Service(@(className)Repository repository) 
            : base(repository)
        {

        }

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="page">page</param>
        /// <param name="size">size</param>
        /// <param name="search">search</param>
        /// <returns></returns>
        public async Task<@("PagingViewModel")> FindListAsync(int page, int size, @(className) search)
        {
            var query = this.Repository.Select
                    @if (!string.IsNullOrWhiteSpace(searchKeyWord))
                    {
<pre>.WhereIf(!string.IsNullOrWhiteSpace(search?.@(searchKeyWord)), w => w.@(searchKeyWord).Contains(search.@(searchKeyWord)))</pre>
                    }
                    .OrderByDescending(w => w.CreationTime)
                    .Select(w => new
                    {
                        w.Id,
                        @(string.Join(',', tableInfos.Select(w => "w." + w.Name)))@(",")
                        LastModificationTime = w.LastModificationTime.ToString("yyyy-MM-dd"),
                        CreationTime = w.CreationTime.ToString("yyyy-MM-dd")
                    })
                ;

            return await this.Repository.AsPagingViewModelAsync(query, page, size);
        }

        /// <summary>
        /// 根据id数组删除
        /// </summary>
        /// <param name="ids">ids</param>
        /// <returns></returns>
        public async Task DeleteListAsync(List<@("Guid")> ids)
        {
            await this.Repository.DeleteByIdsAsync(ids);
        }

        /// <summary>
        /// 查询表单数据
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public async Task<@("Dictionary")<@("string,object")>> FindFormAsync(Guid id)
        {
            var res = new Dictionary<@("string, object")>();
            var form = await this.Repository.FindByIdAsync(id);
            form = form.NullSafe();

            res[nameof(id)] = id == Guid.Empty ? "" : id;
            res[nameof(form)] = form;
            return res;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="form">form</param>
        /// <returns></returns>
        public async Task<@(className)> SaveFormAsync(@className form)
        {
            return await this.Repository.InsertOrUpdateAsync(form);
        }

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public async Task<@("byte[]")> ExportExcelAsync(@className search)
        {
            var tableViewModel = await this.FindListAsync(1, 999999, search);
            return this.ExportExcelByPagingViewModel(tableViewModel, null, "Id");
        }



    }
}
</pre>